package MOLE.Pay;

import MOLE.*;

/** 
 Notice ��ժҪ˵����
*/
public class Alipayreceive
{
	private String key = MOLE.GetValue.GetConfigString("key");
	private String trade_status = MOLE.GetValue.GetRequesValue("trade_status");
//	string  trade_statusx= HttpContext.Current.Request.Form["trade_status"];
	private String money = (MOLE.GetValue.GetRequesValue("total_fee"));
	private String sign = MOLE.GetValue.GetRequesValue("sign");
	private String mysign = "";
	private int IsOk = 0;
	private String demo = "ok";
	private String Oid = String.valueOf(MOLE.GetValue.GetRequesLong("out_trade_no"));
	private String is_success = MOLE.GetValue.GetRequesValue("is_success");
	private double Logistics_fee = MOLE.GetValue.GetRequesDouble("Logistics_fee");
	private String notify_type = MOLE.GetValue.GetRequesValue("notify_type");
	 private String uid = "124";
	private String notify_id = MOLE.GetValue.GetRequesValue("notify_id");
	private String atn = "";
	private String demo_sys = "";
	private String urlreturnok = MOLE.GetValue.GetConfigString("urlreturnok");
	private String ReturnToAlipayProcessResult = "fail";



	public Alipayreceive(int type)
	{
			 mysign = _getMykey(key);
			atn = _GetATN();

			pro();
			  _Rec();

			 HttpContext.Current.Response.Write(ReturnToAlipayProcessResult);


		 //return adn redirect
		 if (type == 2)
		 {
			 String url = urlreturnok + "?msg=" + demo + "&money=" + String.format("%0.2f", MOLE.str.ConverToDecimal(money)) + "&oid=" + Oid.toString();
			 HttpContext.Current.Response.Redirect(url);
		 }


	}



	public final void pro()
	{
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#region
		if (1 == 0)
		{
		}
		 else if (!sign.equals(mysign))
		 {
			demo = "md5�������,��ֵʧ��!������ϵͳ����Ա��ϵ ";
			ReturnToAlipayProcessResult = ("fail");
		 }
//			else if( atn !="true" )  //atn
//			{
//			}
		 else if (!trade_status.equals("TRADE_FINISHED"))
		 {
			demo = "���� trade_status ���� �� TRADE_FINISHED ";
			ReturnToAlipayProcessResult = ("fail");
		 }
		 else
		 {
			 //comple order 
			Order o = new Order(Oid);
			o.Exec();
			if (o.status == 0 || o.status == 2) //successs
			{
				IsOk = 1;
				ReturnToAlipayProcessResult = ("success");
			}
			 demo = o.statusStr;


		 }
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
		///#endregion

	}





	public final void _Rec()
	{
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#region
		try
		{
			MOLE.SQLOS o = new MOLE.SQLOS("netgatelog");
			o.RS("nOid", Oid);
			o.RS("nTradeStatus", trade_status);
			o.RS("nTradeIsSuccess", is_success);
			o.RS("nLogistics_fee ", Logistics_fee);
			o.RS("nNotifytype", notify_type);
			o.RS("nATN ", atn);
			o.RS("nSign ", sign);
			o.RS("nMysign", mysign);
			o.RS("nIsOk", IsOk);
			o.RS("nDemo", demo);
			o.Insert();
			// HttpContext.Current.Response.Write( 	o.InsertSql );
		}
		catch (RuntimeException ee)
		{
			Log.Add("netgatelog  add eerr:  " + ee.toString());

		}


//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#endregion

	}


	public final String _getMykey(String key)
	{
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#region
		System.Collections.Specialized.NameValueCollection coll;
		//Load Form variables into NameValueCollection variable.
		int formparaleng = HttpContext.Current.Request.Form.AllKeys.getLength();
		int questparaleng = HttpContext.Current.Request.QueryString.AllKeys.getLength();
		if (formparaleng > questparaleng) //notify tonzhi
		{
			coll = HttpContext.Current.Request.Form;
		}
		else
		{
			coll = HttpContext.Current.Request.QueryString;
		}

		// Get names of all forms into a string array.
		String[] requestarr = coll.AllKeys;




		String[] textArray2 = MOLE.str.BubbleSort(requestarr);
		String text7 = "";
		for (int i = 0; i < textArray2.length; i++)
		{
			if (((!HttpContext.Current.Request[textArray2[i]].equals("")) && (!textArray2[i].equals("sign"))) && (!textArray2[i].equals("sign_type")))
			{
				if (i == (textArray2.length - 1))
				{
					text7 = text7 + textArray2[i] + "=" + HttpContext.Current.Request[textArray2[i]];
				}
				else
				{
					text7 = text7 + textArray2[i] + "=" + HttpContext.Current.Request[textArray2[i]] + "&";
				}
			}
		}
		String mysign = MOLE.EncryptDecrypt.MD5(text7 + key).toLowerCase();
		return mysign;
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#endregion
	}

	public final String _GetATN()
	{
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#region
		//	HttpContext.Current.Request.Form

	   String text = "https://www.alipay.com/cooperate/gateway.do?" + "service=notify_verify" + "&partner=" + uid + "&notify_id=" + notify_id.trim();

	 String responsetext = MOLE.xmlHttp.Get_Http(text, 0x1d4c0);

	 return responsetext;
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#endregion
	}


}